package com.xltt.newbleclient.bt;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xltt.newbleclient.R;

import java.util.ArrayList;
import java.util.List;


public class BluetoothListAdapter extends CustomBaseAdapter<BluetoothDevice> {

    public interface Listener {
        void onItemClick(BluetoothDevice dev);
    }
    private Listener mListener;
    private Context mContext;
    public BluetoothListAdapter(Context context,Listener listener){
        this.mContext = context;
        this.mListener = listener;
    }

    public BluetoothListAdapter(Context context , List<BluetoothDevice> data,Listener listener){
        this.mContext = context;
        this.mData = data;
        this.mListener = listener;
    }

    public void addBluetoothDevice(BluetoothDevice data){
        if(this.mData == null){
            this.mData = new ArrayList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    public void removeAllBluetoothDevice(){
        mData.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_wifi_list, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.address = (TextView) convertView.findViewById(R.id.address);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BluetoothDevice info = getItem(position);
        holder.name.setText("名称：" + info.getName());
        holder.address.setText("Mac地址：" + info.getAddress());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onItemClick(info);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView name, address;
    }

}
